//ajax
function get(url, data, func) {
    ajax(url, 'GET', data, func);
}

function post(url,data,func) {
    ajax(url, 'POST', data, func);    
}

function patch(url, data, func) {
    ajax(url, 'PATCH', data, func);
}

function ajax(url,method,data,func) {
    $.ajax({
        url: url,
        method: method,
        data: data,
        success: func
    });
}
//window

//message
function message(msg,level) {
    if (level == undefined) {
        $('#message').html(msg);
        $('#message').fadeIn();

        setTimeout(function () {
            $('#message').fadeOut();
        }, 2000);
    } else if (level == 1) {
        alert(msg);
    }    
}

var msg = {
    noSelect: '请选择要修改的项！',
    createFail:'添加失败，请重试！',
    updateSuccess: '修改成功！',
    updateFail:'修改失败，请重试！',
};
//checkbox
$(document.body).on('click', 'input[type="checkbox"]', function () {
    if ($(this).attr('checked') == undefined) {
        $(this).attr('checked', 'checked');
    } else {
        $(this).removeAttr('checked');
    }
});
//pager
function getDefaultPageSize() {
    var size = $.cookie('pageSize');

    if (size == undefined) {
        size = 20;
    }

    return size;
}
//easy ui formatter function
function formatImage(value, row, index) {
    return "<img src='" + value + "' style='height:50px;width:50px;'/>";
}

function formatBool(value, row, index) {
    return value ? "是" : "否";
}

function formatDate(value, row, index) {
    var date = eval("new " + value.replace(/\//g, ''));

    return date.format("yyyy-MM-dd");;
}

function formatTime(value, row, index) {
    var date = value.toDate().format("yyyy-MM-dd hh:mm");

    return date;
}

function formatHour(value, row, index) {
    var days = parseInt(value / 24);
    var hours = value % 24;
    var result = '';

    if (days < 1) {
        result = hours + '小时';
    }
    else {
        if (hours > 0) {
            result = days + '天' + hours + '小时';
        } else {
            result = days + '天';
        }        
    }

    return result;
}

function formatStatus(value, row, index) {
    return value == 1 ? "有效" : "无效";
}
